package com.eleadmin.ocean.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.ocean.biz.OceanBulkCargoProcessBiz;
import com.eleadmin.ocean.entity.OceanBulkCargoProcess;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-11-14 14:03:29
 */
@Repository
public class OceanBulkCargoProcessBizImpl extends BaseDao implements OceanBulkCargoProcessBiz {

    @Override
    public JsonResult list(PageResult2<T> page, OceanBulkCargoProcess oceanBulkCargoProcess, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargoProcess> rm = new BeanPropertyRowMapper<OceanBulkCargoProcess>(OceanBulkCargoProcess.class);
        StringBuffer sql = new StringBuffer("select o.id,o.bulk_cargo_id,o.state");
        sql.append(",o.operate_date");
        sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
        sql.append(",o.update_user");
        sql.append(",suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_ocean_bulk_cargo_process o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=o.update_user ");
        sql.append(" where o.deleted=0 and o.bulk_cargo_id=:bulk_cargo_id ");
        map.put("bulk_cargo_id", oceanBulkCargoProcess.getBulkCargoId());
        if (Utils.isNotEmpty(oceanBulkCargoProcess)) {
            //			if(Utils.isNotEmpty(oceanBulkCargoProcess.getName())){
            //				sql.append(" and instr(o.name,:name)>0 ");
            //				map.put("name", oceanBulkCargoProcess.getName());
            //			}
            if (Utils.isNotEmpty(oceanBulkCargoProcess.getSort()) && Utils.isNotEmpty(oceanBulkCargoProcess.getOrder())) {
                sql.append(" order by o." + oceanBulkCargoProcess.getSort() + " " + oceanBulkCargoProcess.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<OceanBulkCargoProcess> list(OceanBulkCargoProcess oceanBulkCargoProcess, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargoProcess> rm = new BeanPropertyRowMapper<OceanBulkCargoProcess>(OceanBulkCargoProcess.class);
        StringBuffer sql = new StringBuffer("select o.id,o.bulk_cargo_id,o.state");
        sql.append(",o.operate_date");
        sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
        sql.append(",o.update_user");
        sql.append(",suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo_process o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" where o.deleted=0 and o.bulk_cargo_id=:bulk_cargo_id ");
        map.put("bulk_cargo_id", oceanBulkCargoProcess.getBulkCargoId());
        if (Utils.isNotEmpty(oceanBulkCargoProcess)) {
            if (Utils.isNotEmpty(oceanBulkCargoProcess.getSort()) && Utils.isNotEmpty(oceanBulkCargoProcess.getOrder())) {
                sql.append(" order by o." + oceanBulkCargoProcess.getSort() + " " + oceanBulkCargoProcess.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public OceanBulkCargoProcess getById(String id) {
        RowMapper<OceanBulkCargoProcess> rm = new BeanPropertyRowMapper<OceanBulkCargoProcess>(OceanBulkCargoProcess.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.bulk_cargo_id,o.state");
        sql.append(",o.operate_date");
        sql.append(",o.remark,o.create_time,o.create_user,o.update_time");
        sql.append(",o.update_user");
        sql.append(",suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo_process o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" where o.deleted=0 and o.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(OceanBulkCargoProcess oceanBulkCargoProcess, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_ocean_bulk_cargo_process (");
        sql.append("bulk_cargo_id,state,operate_date, remark,create_user,create_time) values ( ");
        sql.append(":bulk_cargo_id,:state,:operate_date,:remark,:create_user,now())");
        map.put("bulk_cargo_id", oceanBulkCargoProcess.getBulkCargoId());
        map.put("state", oceanBulkCargoProcess.getState());
        map.put("operate_date", oceanBulkCargoProcess.getOperateDate());
        map.put("remark", oceanBulkCargoProcess.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(OceanBulkCargoProcess oceanBulkCargoProcess, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_ocean_bulk_cargo_process set ");
        sql.append("bulk_cargo_id=:bulk_cargo_id,state=:state,remark=:remark,operate_date=:operate _date ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("bulk_cargo_id", oceanBulkCargoProcess.getBulkCargoId());
        map.put("state", oceanBulkCargoProcess.getState());
        map.put("remark", oceanBulkCargoProcess.getRemark());
        map.put("operate_date", oceanBulkCargoProcess.getOperateDate());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", oceanBulkCargoProcess.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_ocean_bulk_cargo_process set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
